//
//  CZWelcomeViewController.swift
//  WeiBo
//
//  Created by Apple on 16/9/6.
//  Copyright © 2016年 KY. All rights reserved.
//

import UIKit

//  欢迎页面
class CZWelcomeViewController: UIViewController {

    //  背景图片
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //  头像
    private lazy var headImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        imageView.layer.cornerRadius = imageView.width / 2
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    //  消息文本
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "欢迎您回来，哈哈"
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.darkGray
        return label
    }()
    
    //  自定义视图
    override func loadView() {
        //  设置自定义视图
        view = bgImageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
    }
    
    //  页面已经显示出来，让其执行动画
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    //  开始执行动画
    private func startAnimation() {
    
        //  消息文本框改成透明
        messageLabel.alpha = 0
        //  更新约束
        headImageView.snp_updateConstraints { (make) in
            make.top.equalTo(view).offset(100)
        }
        
        //  usingSpringWithDamping阻尼，取值范围是0-1
        //  initialSpringVelocity 初始速度
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            
            self.view.layoutIfNeeded()
            
            
            }) { (_) in
                
                //  让其消息label的透明度改为1
                UIView.animate(withDuration: 1, animations: { 
                    self.messageLabel.alpha = 1
                    }, completion: { (_) in
                        //  动画执行完成进入首页
                        NotificationCenter.default.post(name: NSNotification.Name(SwitchRootVCNotification), object: nil)
                })
                
                
        }
        
    }
    
    
    
    //  添加控件设置约束
    private func setupUI() {
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        headImageView.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            
        }
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(10)
        }
        
    }

    
    
    
    
    

}
